﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SampleInventoryHandler : MonoBehaviour {

	//Made the sample inventory a list so that it's easier to manipulate...
	public List<ExogenesisItem> playerInventory = new List<ExogenesisItem>();
	public List<ProfileData> profiles = new List<ProfileData>();

	#region Inventory Window Mode Stuff
	public enum InventoryWindowModes {ITEMS, PROFILES}

	private InventoryWindowModes _currentMode;
	public InventoryWindowModes currentMode{
	
		get {return _currentMode;}
		set{
		
			//TODO: put stuff here to do when switching mode
			_currentMode = value;

			if(_currentMode == InventoryWindowModes.ITEMS)
			{
				for(int i = 0; i < EnableOnItems.Length; i++)
					EnableOnItems[i].SetActive(true);
				for(int i = 0; i < EnableOnProfile.Length; i++)
					EnableOnProfile[i].SetActive(false);
			}
			else
			{
				for(int i = 0; i < EnableOnProfile.Length; i++)
					EnableOnProfile[i].SetActive(true);
				for(int i = 0; i < EnableOnItems.Length; i++)
					EnableOnItems[i].SetActive(false);
			}
		}
	
	}
	#endregion
	
	#region Item Description Panel
	[System.Serializable]
	public class ItemDescriptionPanel{
	
		public GameObject WindowParent;

		public UILabel ItemName;
		public UILabel ItemDescription;
		public UITexture ItemImage;

		public ExogenesisItem AssignedItem = null;

		public void Assign(ExogenesisItem toAssign)
		{
			AssignedItem = toAssign;

			ItemName.text = AssignedItem.Name;
			ItemDescription.text = ItemHelper.SanitizeString(AssignedItem.Description);

			ItemImage.mainTexture = AssignedItem.LoadItemTexture();
		}

		public void ClearData()
		{
			ItemName.text = "";
			ItemDescription.text = "";
			ItemImage.mainTexture = Resources.Load("BG/black", typeof(Texture)) as Texture;
		}

	}

	public ItemDescriptionPanel itemDescriptionPanel;
	#endregion

	#region Profile Description Panel
	[System.Serializable]
	public class ProfileDescriptionPanel{
		public GameObject WindowParent;
		
		public UILabel ProfileName;
		public UILabel ProfileDescription;
		public UITexture ProfileImage;
		
		public ProfileData AssignedProfile = null;
		
		public void Assign(ProfileData profileData)
		{
			AssignedProfile = profileData;
			
			ProfileName.text = AssignedProfile.name;
			ProfileDescription.text = ProfileReader.SanitizeString(AssignedProfile.description);
			
			ProfileImage.mainTexture = AssignedProfile.LoadProfileTexture();
		}
		
		public void ClearData()
		{
			ProfileName.text = "";
			ProfileDescription.text = "";
			ProfileImage.mainTexture = Resources.Load("BG/black", typeof(Texture)) as Texture;
		}
	}
	
	public ProfileDescriptionPanel profileDescriptionPanel;
	#endregion

	#region Combine Panel
	[System.Serializable]
	public class CombinePanel
	{
		public GameObject WindowParent;

		public UITexture mat1;
		public UITexture mat2;

		public ExogenesisItem _material1;
		public ExogenesisItem _material2;

		public void ShowWindow(ExogenesisItem material1)
		{
			WindowParent.SetActive(true);

			_material1 = material1;

			mat1.mainTexture = _material1.LoadItemTexture();
			mat2.gameObject.SetActive(false);
		}

		public void SelectMaterial2(ExogenesisItem material2)
		{
			_material2 = material2;
		
			mat2.mainTexture = _material2.LoadItemTexture();
			mat2.gameObject.SetActive(true);
		}

		public List<ExogenesisItem> GetIngredientsAsList()
		{
			List<ExogenesisItem> ret = new List<ExogenesisItem>();
			ret.Add(_material1);
			ret.Add(_material2);

			return ret;
		}
	}

	public CombinePanel combinePanel;
	#endregion

	public GameObject[] EnableOnItems;
	public GameObject[] EnableOnProfile;

	public GameObject ItemsPanel;
	public GameObject ItemPrefab;

	public GameObject ProfilePrefab;

	private bool repositionFlag = false;

	public MenuView mv;

	void Awake()
	{
		//Always do this on scene start.... 
		//Will load every definition of every item in the game-
		ItemHelper.LoadItemList();
		ProfileReader.LoadProfileList();
	}

	// Use this for initialization
	void Start () {
	
		currentMode = InventoryWindowModes.ITEMS;

		mv = FindObjectOfType<MenuView> ();
	}
	
	// Update is called once per frame
	void Update () {
	
		if(repositionFlag)
		{
			repositionFlag = false;
			ItemsPanel.GetComponent<UIGrid>().repositionNow = true;
		}

	}

	#region Item Panel Functions
	void ClearItemPanel()
	{
		Collider[] trans = ItemsPanel.GetComponentsInChildren<Collider>();

		for(int i = 0; i < trans.Length; i++)
		{
			Destroy(trans[i].gameObject);
		}

		itemDescriptionPanel.ClearData();

		ItemsPanel.transform.localPosition = new Vector3(11.98f,835.0f,0);
		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void SpawnSampleInventory()
	{
		foreach(ExogenesisItem item in playerInventory)
		{
			if(itemDescriptionPanel.AssignedItem.ID == -1)
				itemDescriptionPanel.Assign(item);
			
			GameObject g = Instantiate(ItemPrefab) as GameObject;
			g.transform.parent = ItemsPanel.transform;
			g.transform.localScale = Vector3.one;
			
			g.GetComponent<ItemItemHandler>().Assign(item);
			g.GetComponent<UIButtonMessage>().target = gameObject;
			g.GetComponent<UIButtonMessage>().functionName = "ItemClicked";

		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void SpawnALLItems()
	{
		foreach(var item in ItemHelper.EXOGENESIS_ITEMS)
		{
			if(itemDescriptionPanel.AssignedItem.ID == -1)
				itemDescriptionPanel.Assign(item.Value);

			GameObject g = Instantiate(ItemPrefab) as GameObject;
			g.transform.parent = ItemsPanel.transform;
			g.transform.localScale = Vector3.one;

			g.GetComponent<ItemItemHandler>().Assign(item.Value);
			g.GetComponent<UIButtonMessage>().target = gameObject;
			g.GetComponent<UIButtonMessage>().functionName = "ItemClicked";
		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void ItemClicked(GameObject item)
	{
		print ("mouse detect");
		ExogenesisItem selectedItem = item.GetComponent<ItemItemHandler>().assignedItem;

		if(!combinePanel.WindowParent.activeSelf)
			itemDescriptionPanel.Assign(selectedItem);
		else
			combinePanel.SelectMaterial2(selectedItem);
	}
	
	void CombinePress()
	{
		combinePanel.ShowWindow(itemDescriptionPanel.AssignedItem);
		itemDescriptionPanel.WindowParent.SetActive(false);
	}

	void ConfirmCombine()
	{
		ItemCombination combi = ItemHelper.GetItemCombination(combinePanel.GetIngredientsAsList());
		if(combi != null)
		{
			ExogenesisItem product = combi.CombineItems(playerInventory);

			ClearItemPanel();
			SpawnSampleInventory();

			ItemsPanel.GetComponent<UIGrid>().repositionNow = true;
			repositionFlag = true;
			Debug.Log("Combination Success! Got " + product.Name);

			itemDescriptionPanel.Assign(product);
		}
		else
		{
			Debug.Log("No combination found");
		}

		itemDescriptionPanel.WindowParent.SetActive(true);
		combinePanel.WindowParent.SetActive(false);
	}
	#endregion

	#region Profile Panel Functions
	void spawnProfiles()
	{
		foreach(ProfileData pd in profiles)
		{
			if(profileDescriptionPanel.AssignedProfile.ID == -1)
				profileDescriptionPanel.Assign(pd);
			
			GameObject go = Instantiate(ProfilePrefab) as GameObject;
			go.transform.parent = ItemsPanel.transform;
			go.transform.localScale = Vector3.one;
			
			go.GetComponent<ProfileHandler>().Assign(pd);
			go.GetComponent<UIButtonMessage>().target = gameObject;
			go.GetComponent<UIButtonMessage>().functionName = "ProfileClicked";
			
		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void spawnAllProfiles()
	{
		foreach (var item in ProfileReader.EXOGENESIS_PROFILES)
		{
			if(profileDescriptionPanel.AssignedProfile.ID == -1)
				profileDescriptionPanel.Assign(item.Value);

			GameObject go = Instantiate(ProfilePrefab) as GameObject;
			go.transform.parent = ItemsPanel.transform;
			go.transform.localScale = Vector3.one;
			
			go.GetComponent<ProfileHandler>().Assign(item.Value);
			go.GetComponent<UIButtonMessage>().target = gameObject;
			go.GetComponent<UIButtonMessage>().functionName = "ProfileClicked";
		}

		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}

	void ProfileClicked(GameObject item)
	{
		Debug.Log("Mouse Detect");
		ProfileData selectedItem = item.GetComponent<ProfileHandler>().assignedProfile;

		profileDescriptionPanel.Assign(selectedItem);
	}

	void ClearProfilePanel()
	{
		Collider[] trans = ItemsPanel.GetComponentsInChildren<Collider>();

		for (int i = 0; i < trans.Length; i++)
		{
			Destroy(trans[i].gameObject);
		}

		profileDescriptionPanel.ClearData();

		ItemsPanel.transform.localPosition = new Vector3(11.98f, 420.0f, 0);
		ItemsPanel.GetComponent<UIGrid>().Reposition();
	}
	#endregion

	void MainInventoryButtonClick(GameObject sender)
	{
		if(sender.name.Contains("Item"))
		{
			currentMode = InventoryWindowModes.ITEMS;
			ClearProfilePanel();
			SpawnSampleInventory();
		}
		else
		{
			currentMode = InventoryWindowModes.PROFILES;
			ClearItemPanel();
			spawnProfiles();
		}
	}

	public void Activate()
	{
		currentMode = InventoryWindowModes.ITEMS;

		SpawnSampleInventory();
	}

	void OnBack(GameObject sender)
	{
		GameObject.Find ("InventoryPanel").SetActive (false);
		mv.UIMenu.Find (x => x.name == "ItemBoxPanel").SetActive (true);
	}

	public void LoadFromSaveFile(List<int> inventory, List<int> profile, List<string> profileDescs)
	{
		//Clear contents of Player Inventory and Profiles
		playerInventory.Clear();
		profiles.Clear();

		//Load the contents from the save file

		//Load the obtained items
		for(int i = 0; i < inventory.Count; i++)
			playerInventory.Add(ItemHelper.GetItemByID(inventory[i]));

		//Load the previous profiles
		for(int i = 0; i < profile.Count; i++)
		{
			profiles.Add(ProfileReader.GetProfileByID(profile[i]));
			profiles[i].description = Profiles.UpdateDescription(profiles, profiles[i].name, profileDescs[i]);
		}
	}
}

